import os
import socket as sk
import time

server = '127.0.0.1'
port = 5000


try:
	with open('user.info','r')as f:
		user = f.read()
except:
	user = ''

def sendFile(fileName,towho):
	global user
	s = sk.socket(sk.AF_INET,sk.SOCK_STREAM)
	s.connect((server,port))
	s.sendall(bytes('send|'+towho,'utf-8'))
	time.sleep(0.1)
	with open(fileName,'rb') as f:
		s.sendall(f.read())



	

	
def recvFile(saveName):
	global user
	s = sk.socket(sk.AF_INET,sk.SOCK_STREAM)
	s.connect((server,port))
	s.sendall(bytes('recv|'+user,'utf-8'))
	if os.path.exists(saveName):
		os.remove(saveName)
	with open(saveName,'wb') as f:
		while True:
			d = s.recv(1024)
			if d:
				f.write(d)
			else:
				break


def check():#check if there is a file to recv
	global user
	s = sk.socket(sk.AF_INET,sk.SOCK_STREAM)
	s.connect((server,port))
	s.sendall(bytes('check|'+user,'utf-8'))
	status = str(s.recv(1024),'utf-8')
	if status=='yes':
		return True
	if status=='no':
		return False

if __name__ == '__main__':
	# sendFile('F:/python/test.csv','test')
	pass















